package com.zhoux.ty.web.controller;

import com.zhoux.ty.domain.JsonResult;
import com.zhoux.ty.domain.SysNotice;
import com.zhoux.ty.service.SysNoticeService;
import com.zhoux.ty.web.util.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author taoyi
 * @version 1.0
 * @date 2021/7/24 22:20
 */
@Api(tags = "公告控制器")
@RestController
@RequestMapping("/notice/")
public class SysNoticeController {

    @Autowired
    private SysNoticeService sysNoticeService;


    /**
     * 测试
     * @return
     */
//    @ApiOperation(value = "查询所有", notes = "查询所有信息")
//    @RequestMapping("/test")
//    public JsonResult test(){
//        List<SysNotice> sysNotices = sysNoticeService.selectNotices(null);
//        return new JsonResult(sysNotices);
//    }

    @ApiOperation(value = "新增公告", notes = "新增公告信息")
    @PostMapping("/doInsertNotice")
    public JsonResult doInsertNotice(@RequestBody SysNotice notice){
        sysNoticeService.insertNotice(notice);
        return new JsonResult("insert ok");
    }

    /**
     * @CacheEvict 表示清除缓存,value的值为缓存名称,
     * allEntries=true表示清除所有,
     * beforeInvocation是在方法执行之前还是之后执行
     * @param notice
     * @return
     */
    //@CacheEvict(value="notices",allEntries = true,beforeInvocation = false)
    @PutMapping("doUpdateNotice")
    @ApiOperation(value = "更新公告", notes = "更新公告信息")
    public JsonResult doUpdateNotice(@RequestBody SysNotice notice){
        sysNoticeService.updateNotice(notice);
        return new JsonResult("update ok");
    }

    @ApiOperation(value = "删除公告", notes = "根据id删除公告信息")
    @DeleteMapping("doDeleteById/{id}")
    public JsonResult doDeleteById(@PathVariable Long ...id){
        sysNoticeService.deleteById(id);
        return new JsonResult("delete ok");
    }

    /*
     * @Cacheable 描述方法时,系统底层在执行时会将查询结果存储到缓存(本地缓存,redis),
     * 下次查询时会直接从缓存查询,来提高查询性能.
     * @param id
     * @return
     */
//    @RequiredLog(operation="基于id查询通告信息")
//    @Cacheable(value = "notices")
    @GetMapping("doSelectById/{id}")
    @ApiOperation(value = "查询公告", notes = "根据id查询公告信息")
    public JsonResult doSelectById(@PathVariable Long id){
        return new JsonResult(sysNoticeService.selectById(id));
    }

    /**
     * @Cacheable 这个注解描述的方法,在执行时,系统底层会先去查询缓存
     * @param sysNotice
     * @return
     */
    //@RequiredLog(operation="查询通告信息")
    //@Cacheable(value = "notices")//这里notices表示缓存的名称
    @GetMapping("doSelectNotices")
    @ApiOperation(value = "查询公告", notes = "分页查询所有公告信息")
    public JsonResult doSelectNotices(SysNotice sysNotice){
        //上面分页的简化写法就是如下方式
        return new JsonResult(PageUtils.startPage()//启动分页拦截器
                .doSelectPageInfo(()->{//执行查询业务
                    sysNoticeService.selectNotices(sysNotice); }));
    }

}
